﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Media;
using System.Xml.Serialization;
using System.Xml;

[Serializable]
    public class Library : PlayList
    {
        #region "Extensions"
        private String[] musicExt = {".mp3", ".wav", ".wma", ".ogg" };
        private String[] movieExt = {".wmv", ".avi", ".mkv", ".mp4", ".mov" };
        private String[] pictureExt = {".jpg", ".jpeg", ".bmp", ".png", ".gif" };
        #endregion

        #region "Filtres"
    /// <summary>
    /// Filter the list of media by album
    /// </summary>
        public void filterByAlbum()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getAlbum())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getAlbum())))
                    .OrderBy(v => v.getAlbum());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
            }
        }

    /// <summary>
        /// Filter the list of media by album descending
    /// </summary>
        public void filterByAlbumDesc()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getAlbum())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getAlbum())))
                    .OrderByDescending(v => v.getAlbum());
                listMedia.Clear();
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }

    /// <summary>
        /// Filter the list of media by artist
    /// </summary>
        public void filterByArtist()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getAuthor())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getAuthor())))
                    .OrderBy(v => v.getAuthor());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
            }
        }

    /// <summary>
    /// Filter the list of media by artist descending
    /// </summary>
        public void filterByArtistDesc()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getAuthor())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                   .FindAll(v => !(string.IsNullOrEmpty(v.getAuthor())))
                   .OrderByDescending(v => v.getAuthor());
                listMedia.Clear();
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }

    /// <summary>
    /// Filter the list of media by title
    /// </summary>
        public void filterByTitle()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getTitle())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getTitle())))
                    .OrderBy(v => v.getTitle());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
            }
        }
    /// <summary>
    /// Filter the list of media by title desc
    /// </summary>
        public void filterByTitleDesc()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getTitle())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getTitle())))
                    .OrderByDescending(v => v.getTitle());
                listMedia.Clear();
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }
    /// <summary>
    /// Filter the list of media by lenght descending
    /// </summary>
        public void filterByLengthDesc()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getLength())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getLength())))
                    .OrderByDescending(v => v.getLength());
                listMedia.Clear();
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }
    /// <summary>
    /// Filter the list of media by year
    /// </summary>
        public void filterByYear()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getYear())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getYear())))
                    .OrderByDescending(v => v.getYear());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
            }
        }

    /// <summary>
    /// Filter the list of media by year descending
    /// </summary>
        public void filterByYearDesc()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getYear())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getYear())))
                    .OrderByDescending(v => v.getYear());
                listMedia.Clear();
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }
    /// <summary>
    /// Filter the list of media filter by lenght
    /// </summary>
        public void filterByLength()
        {
            if (listMedia != null)
            {
                List<AMediaInfo> nullValues = listMedia.FindAll(v => (string.IsNullOrEmpty(v.getLength())));
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia
                    .FindAll(v => !(string.IsNullOrEmpty(v.getLength())))
                    .OrderBy(v => v.getLength());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
                if (nullValues != null)
                    listMedia.AddRange(nullValues);
            }
        }
/// <summary>
/// Filter the list of media by size
/// </summary>
        public void filterBySize()
        {
            if (listMedia != null)
            {
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia.FindAll(v => true)
                    .OrderBy(v => v.getSize());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }

    /// <summary>
    /// Filter the list of media by size descending
    /// </summary>
        public void filterBySizeDesc()
        {
            if (listMedia != null)
            {
                System.Linq.IOrderedEnumerable<AMediaInfo> otherValues = listMedia.FindAll(v => true)
                    .OrderByDescending(v => v.getSize());
                listMedia.Clear();
                if (otherValues != null)
                    listMedia.AddRange(otherValues);
            }
        }
        #endregion

        #region "Categories"

    /// <summary>
    /// Get all music from the library
    /// </summary>
    /// <returns>list of all musics</returns>
        public List<AMediaInfo> getAllMusic()
        {
            if (listMedia != null)
            {
                return (listMedia.FindAll(v => v is MusicInfo));
            }
            return (null);
        }
        /// <summary>
        /// Get all movie from the library
        /// </summary>
        /// <returns>list of all movie</returns>
        public List<AMediaInfo> getAllMovie()
        {
            if (listMedia != null)
            {
                return (listMedia.FindAll(v => v is MovieInfo));
            }
            return (null);
        }
        /// <summary>
        /// Get all picture from the library
        /// </summary>
        /// <returns>list of all picture</returns>
        public List<AMediaInfo> getAllPicture()
        {
            if (listMedia != null)
            {
                return (listMedia.FindAll(v => v is PictureInfo));
            }
            return (null);
        }
        #endregion

        #region "Constructeur"
    /// <summary>
    /// Constructor of the library
    /// </summary>
        public Library() : base("Library")
        {
        }
        #endregion

        #region "Ajout / Suppression donnees"
    /// <summary>
    /// Get all media info from the windows native library folders like My Music, My Movie, My Pictures
    /// </summary>
        public void importFilesFromLibraryFolders()
        {
            String musicPath = "C:/Users/" + Environment.UserName + "/Music/";
            String moviePath = "C:/Users/" + Environment.UserName + "/Videos/";
            String picturePath = "C:/Users/" + Environment.UserName + "/Pictures/";
            getMediaInfoFromFolder(musicPath);
            getMediaInfoFromFolder(moviePath);
            getMediaInfoFromFolder(picturePath);
        }
        #endregion

        #region "Serialization"
    /// <summary>
    /// Serialize the library by creating a list of Media's path
    /// </summary>
        public override void serialize()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Library));
            TextWriter textWriter = new StreamWriter("serialization.xml");
            if (serializer != null && textWriter != null)
                serializer.Serialize(textWriter, this);
            if (textWriter != null)
                textWriter.Close();
        }
        #endregion
    }